<template>
    <view class="addDizhi">
        <view class="list">
            <view class="item">
                <view class="item1">
                    <text>收货人</text>
                    <input type="text" placeholder="请输入姓名" v-model="address.name" />
                </view>
                <view class="item1">
                    <text>手机号码</text>
                    <input type="text" placeholder="请输入手机号" v-model="address.phone" />
                </view>
                <view class="item1">
                    <text>地图定位</text>
                    <view class="item_right" @click="openMap()">
                        <input type="text" placeholder="请选择" disabled="true" />
                        <image class="img" src="@/static/mine/more.png" mode="widthFix"></image>
                    </view>
                </view>
                <view class="item1">
                    <text>详细地址</text>
                    <input type="text" placeholder="填写详细门牌号" v-model="address.dizhi" />
                </view>
            </view>
            <view class="item2">
                <text>设为默认地址</text>
                <u-switch activeColor="#DF4647" v-model="address.show" @change="change"></u-switch>
            </view>
            <view class="btn" @click="addAddress()"> 保存 </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            address: {
                name: "",
                phone: "",
                dizhi: "",
                show: false,
            },
        };
    },
    methods: {
        openMap() {
            uni.getLocation({
                type: "wgs84",
                success: (res) => {
                    const latitude = res.latitude;
                    const longitude = res.longitude;
                    uni.openLocation({
                        latitude: latitude,
                        longitude: longitude,
                        scale: 18,
                        success() {
                            console.log("地图打开成功");
                        },
                        fail() {
                            console.log("地图打开失败");
                        },
                    });
                },
                fail: () => {
                    uni.showToast({
                        title: "无法获取位置信息",
                        icon: "none",
                    });
                },
            });
        },
        change(e) {
            console.log("change", e);
        },
        addAddress() {
            // 获取当前已有的地址列表
            let addresses = uni.getStorageSync("addresses") || [];
            // 添加新地址到列表
            addresses.push(this.address);
            // 重新存储地址列表
            uni.setStorageSync("addresses", addresses);
            uni.showToast({
                //（改）
                title: "添加地址成功",
                icon: "success",
                duration: 2000,
            });
            setTimeout(() => {
                uni.navigateBack();
            }, 2000);
        },
    },
    onReady() {},
};
</script>

<style lang="scss" scoped>
.addDizhi {
    width: 100%;
    min-height: 100vh;
    overflow: hidden;
    background-color: #f7f7f7;

    .list {
        width: 94%;
        padding: 20rpx 0rpx;
        margin: 0 auto;
        overflow: hidden;
        margin-bottom: 130rpx;
        .item {
            margin-top: 20rpx;
            padding: 10rpx 30rpx;
            background: #ffffff;
            border-radius: 20rpx;

            .item1:last-child {
                border: none;
            }

            .item1 {
                padding: 30rpx 10rpx;
                border-bottom: 1px solid #e5e5e5;
                display: flex;

                .item_right {
                    width: 470rpx;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                }

                text {
                    font-weight: 500;
                    font-size: 30rpx;
                    color: #333333;
                    margin-right: 10rpx;
                    width: 150rpx;
                }

                .img {
                    width: 13rpx;
                }
            }
        }

        .item2 {
            margin-top: 20rpx;
            padding: 30rpx 30rpx;
            background: #ffffff;
            border-radius: 20rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-weight: 500;
            font-size: 28rpx;
            color: #333333;
        }

        .btn {
            width: 94%;
            margin: 0 auto;
            padding: 20rpx 0;
            text-align: center;
            color: #fff;
            background-color: #df4647;
            border-radius: 50rpx;
            position: fixed;
            bottom: 80rpx;
            font-weight: 500;
            font-size: 30rpx;
        }
    }
}
</style>
